/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.http.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
import org.apache.http.protocol.HttpContext;

/**
 *
 * @author hstel-hxg
 */
public abstract class NamedRequestHandler implements HttpAsyncRequestHandler<HttpRequest> {

    @Override
    public HttpAsyncRequestConsumer<HttpRequest> processRequest(HttpRequest hr, HttpContext hc) throws HttpException, IOException {
        return new BasicAsyncRequestConsumer();
    }

    public String getName() {
        
        return this.getClass().getSimpleName();
    }

    public Map<String, List<String>> getParamsModeGet(HttpRequest request) {
        Map<String, List<String>> mp = new HashMap<>();
        String uri = request.getRequestLine().getUri();
        //System.out.println(uri);
        if (uri.indexOf('?') > -1) {
            String[] params = uri.substring(uri.indexOf('?') + 1).split("&");
            for (String param : params) {
                String[] ss = param.split("=");
                if (ss.length >= 2) {
                    List<String> list = mp.getOrDefault(ss[0], new ArrayList<>());
                    list.add(ss[1]);
                    mp.put(ss[0], list);
                }
            }
        }
        return mp;
    }

    public Map<String, List<String>> getParamsModePost(BasicHttpEntityEnclosingRequest request) throws IOException {
        Map<String, List<String>> mp = new HashMap<>();
        //String uri = request.getRequestLine().getUri();
        //System.out.println(uri);
        InputStream is = request.getEntity().getContent();
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        String[] params = (new String(bytes)).split("&");
        for (String param : params) {
            String[] ss = param.split("=");
            if (ss.length >= 2) {
                List<String> list = mp.getOrDefault(ss[0], new ArrayList<>());
                list.add(ss[1]);
                mp.put(ss[0], list);
            }
        }
        return mp;
    }

    public String getHeaderInfo(HttpRequest request) {
        final StringBuilder sb=new StringBuilder();
        Arrays.stream(request.getAllHeaders()).forEach((head) ->{
            sb.append(head.getName()).append(":").append(head.getValue());
        });
        return sb.toString();
    }

    @Override
    public String toString() {
        return getName();
    }

}
